home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1996 June / EnigmA AMIGA RUN 08 (1996)(G.R. Edizioni)(IT)[!][issue 1996-06][EARSAN CD VII].iso / earcd / cmdity / cntrwn23.lha / CenterWin / CenterWin.c < prev    next >
C/C++ Source or Header  |  1996-04-30  |  9KB  |  398 lines

  1. /*
  2. **  $VER: CenterWindow 2.3 (30 Apr 1996)  **
  3. **
  4. **        © 1996 Timo C. Nentwig
  5. **          all rights reserved !
  6. **
  7. ** ======================================
  8. **
  9. **  Language:
  10. **  ¯¯¯¯¯¯¯¯
  11. **
  12. **    Program is compiled by SAS/C
  13. **
  14. **
  15. **  Purpose:
  16. **  ¯¯¯¯¯¯¯
  17. **
  18. **    Center the active window
  19. **    by hotkey.
  20. **
  21. **    Put window under window -> Parent
  22. **    by hotkey.
  23. **
  24. **  Requirements:
  25. **  ¯¯¯¯¯¯¯¯¯¯¯¯
  26. **
  27. **    AmigaOS v37+
  28. **
  29. **  Bugs:
  30. **  ¯¯¯¯
  31. **
  32. **
  33. **
  34. **  ToDo:
  35. **  ¯¯¯¯
  36. **
  37. **
  38. **
  39. **  Notes:
  40. **  ¯¯¯¯¯
  41. **
  42. **
  43. ** ======================================
  44. **
  45. **  History:
  46. **  ¯¯¯¯¯¯¯
  47. **
  48. **  01 Apr 1996 - 1.0 : initial release
  49. **  26 Apr 1996 - 2.0 : completely overworked (name changed)
  50. **                      now works by hotkey
  51. **  27 Apr 1996 - 2.1 : HOTKEY -> CENTERKEY
  52. **                      add: PARENTKEY -> put window under window -> parent
  53. **
  54. **  27 Apr 1996 - 2.2 : PARENTKEY: Care if win -> Parent has WFLG_DRAGBAR
  55. **                      add: Tooltype FULLHEIGHT
  56. **
  57. **  30 Apr 1996 - 2.3 : add: ZIPKEY
  58. **
  59. */
  60.  
  61. /// #include
  62.  
  63. #include <dos/dos.h>
  64. #include <exec/types.h>
  65. #include <intuition/intuition.h>
  66. #include <intuition/intuitionbase.h>
  67.  
  68. #include <libraries/commodities.h>
  69.  
  70. #include <proto/commodities.h>
  71. #include <proto/exec.h>
  72. #include <proto/intuition.h>
  73.  
  74. #include <stdio.h>
  75. #include <strings.h>
  76.  
  77. ///
  78. /// #define
  79.  
  80. #define EVT_CENTERKEY  1L
  81. #define EVT_PARENTKEY  2L
  82. #define EVT_ZIPKEY     3L
  83.  
  84. #define PRG_VERSION "2.3"
  85. #define PRG_TITLE   "CenterWin"
  86. #define PRG_AUTHOR  "Timo C. Nentwig"
  87. #define PRG_YEAR    "1996"
  88.  
  89. ///
  90. /// Prototypes
  91.  
  92. VOID      ProcessMsg      (VOID);
  93. BOOL      AttachFilter    (STRPTR onkey, ULONG user_event);
  94.  
  95. ///
  96.  
  97. struct    IntuitionBase   *IntuitionBase;
  98. struct    Library         *CxBase;
  99. struct    MsgPort         *broker_mp;
  100. CxObj                     *broker;
  101. ULONG                      cxsigflag;
  102. BOOL                       fullheight;
  103.  
  104. /// main ()
  105.  
  106. VOID
  107. main (UWORD argc, STRPTR *argv)
  108. {
  109.  
  110.     if (IntuitionBase = (struct IntuitionBase *) OpenLibrary ("intuition.library", 37))
  111.     {
  112.  
  113.         if (CxBase = OpenLibrary ("commodities.library", 37))
  114.         {
  115.  
  116.             if (broker_mp = CreateMsgPort())
  117.             {
  118.  
  119.                 STRPTR   *ttypes;
  120.                 STRPTR    centerkey;
  121.                 STRPTR    parentkey;
  122.                 STRPTR    zipkey;
  123.                 struct    NewBroker    newbroker =
  124.                 {
  125.  
  126.                     NB_VERSION,
  127.                     PRG_TITLE,
  128.                     PRG_TITLE" "PRG_VERSION" © "PRG_YEAR" by "PRG_AUTHOR,
  129.                     "Center window by hotkey",
  130.                     NBU_UNIQUE | NBU_NOTIFY,
  131.                     0, 0, 0, 0
  132.  
  133.                 };
  134.  
  135.                 newbroker.nb_Port = broker_mp;
  136.                 cxsigflag         = 1L << broker_mp->mp_SigBit;
  137.  
  138.                 ttypes            = ArgArrayInit (argc, argv);
  139.                 newbroker.nb_Pri  = (BYTE) ArgInt (ttypes, "CX_PRIORITY", 0);
  140.                 centerkey         =     ArgString (ttypes, "CENTERKEY",   "F1");
  141.                 parentkey         =     ArgString (ttypes, "PARENTKEY",   "F2");
  142.                 zipkey            =     ArgString (ttypes, "ZIPKEY",      "F3");
  143.  
  144.                 if (stricmp (ArgString (ttypes, "FULLWIDTH", "NO"), "NO") == 0)
  145.                     fullheight = FALSE;
  146.                 else
  147.                     fullheight = TRUE;
  148.  
  149.                 if (broker = CxBroker (&newbroker, NULL))
  150.                 {
  151.  
  152.                     CxMsg   *msg;
  153.  
  154.                     if (AttachFilter (centerkey, EVT_CENTERKEY) &&
  155.                         AttachFilter (parentkey, EVT_PARENTKEY) &&
  156.                         AttachFilter (zipkey,    EVT_ZIPKEY))
  157.                     {
  158.  
  159.                         ActivateCxObj (broker, 1L);
  160.                         ProcessMsg();
  161.  
  162.                     }
  163.  
  164.                     DeleteCxObjAll (broker);
  165.  
  166.                     while (msg = (CxMsg *) GetMsg (broker_mp))         // Empty the port of all CxMsgs
  167.                         ReplyMsg ((struct Message *) msg);
  168.  
  169.                 }
  170.  
  171.                 DeletePort(broker_mp);
  172.  
  173.             }
  174.  
  175.             ArgArrayDone();
  176.             CloseLibrary (CxBase);
  177.  
  178.         }
  179.         else
  180.         {
  181.  
  182.             printf ("ERROR: Couldn't open commodities.library v37+\n");
  183.  
  184.         }
  185.  
  186.         CloseLibrary ((struct Library *) IntuitionBase);
  187.  
  188.     }
  189.     else
  190.     {
  191.  
  192.         printf ("ERROR: Couldn't open intuition.library v37+\n");
  193.  
  194.     }
  195.  
  196. }
  197.  
  198. ///
  199. /// ProcessMsg ()
  200.  
  201.     /*
  202.      *    FROM        /rkm/libs/commodities/hotkey.c
  203.      *
  204.      *    FUNCTION    Process commodity messages.
  205.      *
  206.      *    NOTE
  207.      *
  208.      *    EXAMPLE     ProcessMsg ();
  209.      *
  210.      */
  211.  
  212.  
  213. VOID
  214. ProcessMsg (VOID)
  215. {
  216.  
  217.     CxMsg   *msg;
  218.     ULONG    sigrcvd;
  219.     ULONG    msgid;
  220.     ULONG    msgtype;
  221.     LONG     returnvalue = 1L;
  222.  
  223.     while (returnvalue)
  224.     {
  225.  
  226.         sigrcvd = Wait (SIGBREAKF_CTRL_C | cxsigflag);
  227.  
  228.         while (msg = (CxMsg *) GetMsg (broker_mp))
  229.         {
  230.  
  231.             msgid   = CxMsgID   (msg);
  232.             msgtype = CxMsgType (msg);
  233.             ReplyMsg ((struct Message *) msg);
  234.  
  235.             switch (msgtype)
  236.             {
  237.  
  238.                 case CXM_IEVENT:
  239.  
  240.                     switch (msgid)
  241.                     {
  242.  
  243.                         case EVT_CENTERKEY:
  244.  
  245.                             {
  246.  
  247.                                 struct    Window   *win = IntuitionBase -> ActiveWindow;
  248.                                 UWORD               plus;
  249.  
  250.                                 if (fullheight)                                                // accept titlebar
  251.                                     plus = win -> Parent -> WScreen -> Font -> ta_YSize + 3;
  252.                                 else
  253.                                     plus = 0;
  254.  
  255.                                 MoveWindow (win, (((win -> WScreen -> Width          - win -> Width ) / 2) - win -> LeftEdge),
  256.                                                 ((((win -> WScreen -> Height - plus) - win -> Height) / 2) - win ->  TopEdge));
  257.  
  258.                             }
  259.                             break;
  260.  
  261.                         case EVT_PARENTKEY:
  262.  
  263.                             {
  264.  
  265.                                 struct    Window   *win = IntuitionBase -> ActiveWindow;
  266.                                 UWORD               plus;
  267.  
  268.                                 if (win -> Parent -> Flags &= WFLG_DRAGBAR)                    // parent has a dragbar
  269.                                     plus = win -> Parent -> WScreen -> Font -> ta_YSize + 3;
  270.                                 else
  271.                                     plus = 0;
  272.  
  273.  
  274.                                 if (win -> Parent)
  275.                                 {
  276.  
  277.                                     MoveWindow (win, win -> Parent -> LeftEdge - win -> LeftEdge,
  278.                                                     (win -> Parent -> TopEdge -  win -> TopEdge) + plus);
  279.  
  280.                                 }
  281.                                 else
  282.                                 {
  283.                                     // put to 0,0
  284.                                     MoveWindow (win, 0 - win -> LeftEdge,
  285.                                                      0 - win ->  TopEdge);
  286.  
  287.                                 }
  288.  
  289.                             }
  290.                             break;
  291.  
  292.                         case EVT_ZIPKEY:
  293.  
  294.                             {
  295.  
  296.                                 struct    Window   *win = IntuitionBase -> ActiveWindow;
  297.  
  298.                                 ZipWindow (win);
  299.  
  300.                             }
  301.                             break;
  302.  
  303.                     }
  304.                     break;
  305.  
  306.                 case CXM_COMMAND:
  307.  
  308.                     switch (msgid)
  309.                     {
  310.  
  311.                         case CXCMD_DISABLE:
  312.  
  313.                             ActivateCxObj (broker, 0L);
  314.                             break;
  315.  
  316.                         case CXCMD_ENABLE:
  317.  
  318.                             ActivateCxObj (broker, 1L);
  319.                             break;
  320.  
  321.                         case CXCMD_KILL:
  322.  
  323.                             returnvalue = 0L;
  324.                             break;
  325.  
  326.                         case CXCMD_UNIQUE:
  327.  
  328.                             returnvalue = 0L;
  329.                             break;
  330.  
  331.                     }
  332.                     break;
  333.  
  334.             }
  335.  
  336.         }
  337.  
  338.         if (sigrcvd & SIGBREAKF_CTRL_C)
  339.             returnvalue = 0L;
  340.  
  341.     }
  342.  
  343. }
  344.  
  345. ///
  346. /// AttachFilter ()
  347.  
  348.     /*
  349.      *    FROM        /CloseWB/CloseWB.c
  350.      *
  351.      *    FUNCTION    Attach a filter.
  352.      *
  353.      *    NOTE
  354.      *
  355.      *    EXAMPLE     AttachFilter (hotkey, EVT_HOTKEY);
  356.      *
  357.      */
  358.  
  359.  
  360. BOOL
  361. AttachFilter (STRPTR onkey, ULONG user_event)
  362. {
  363.  
  364.     CxObj   *filter;
  365.     CxObj   *sender;
  366.     CxObj   *translator;
  367.  
  368.  
  369.     if (filter = CxFilter (onkey))
  370.     {
  371.  
  372.         AttachCxObj (broker, filter);
  373.  
  374.        if (sender = CxSender (broker_mp, user_event))
  375.        {
  376.  
  377.            AttachCxObj (filter, sender);
  378.  
  379.            if (translator = CxTranslate (NULL))
  380.            {
  381.  
  382.                AttachCxObj (filter, translator);
  383.  
  384.                if ( ! (CxObjError (filter)))
  385.                    return (TRUE);
  386.  
  387.            }
  388.  
  389.        }
  390.  
  391.     }
  392.  
  393.     return (FALSE);
  394.  
  395. }
  396.  
  397. ///
  398.